#include "net.hpp"
#include "abstract.hpp"
#include "message.hpp"
#include "dispatcher.hpp"
#include "../server/rpc_server.hpp"
// void onMessage(const bitrpc:: BaseConnection::ptr& conn,bitrpc:: RPCRequest::ptr & msg)
// {
//      DLOG("rpc请求来啦");
//      std::string body = msg->Serialize();
//      std::cout<<body<<std::endl;
     
//      auto req = bitrpc::MessageFactory::create<bitrpc::RpcResponse>();
//      req->SetId("1111");
//      req->SetMessageType(bitrpc::MessageType::RSP_RPC);
//      req->setRcode(bitrpc::RCODE::RCODE_OK);
//      req->setResult(33);
//      conn->send(req);
// }
void Add(const Json::Value& req,Json::Value& result)
{
    DLOG("开始运算");
    int num1 = req["num1"].asInt();
    DLOG("num1:%d",num1);
    int num2 = req["num2"].asInt();
    DLOG("num2:%d",num2);

    result = num1 + num2;
    std::cout<<result<<std::endl;
}
int main()
{
  //   auto disp = bitrpc::MessageFactory::create<bitrpc::dispatcher>();
  // // disp->registerHandler<bitrpc::RPCRequest>(bitrpc::MessageType::REQ_RPC,onMessage);
  //   auto routor = std::make_shared<bitrpc::server::RpcRoutor>();
  //   auto cb  = std::bind(&bitrpc::server::RpcRoutor::onMessage,routor.get(),std::placeholders::_1,std::placeholders::_2);
    // std::unique_ptr<bitrpc::server::ServiceFactory> desc_factory(std::make_unique<bitrpc::server::ServiceFactory>());
    // desc_factory->setMethodName("Add");
    // desc_factory->setParamsDesc("num1",bitrpc::server::VType::INTEGRAL);
    // desc_factory->setParamsDesc("num2",bitrpc::server::VType::INTEGRAL);
    // desc_factory->returnType(bitrpc::server::VType::INTEGRAL);
    // desc_factory->setCallback(Add);
  //   routor->Register(desc_factory->build());

  //   disp->registerHandler<bitrpc::RPCRequest>(bitrpc::MessageType::REQ_RPC,cb);
  //   auto method = std::bind(&bitrpc::dispatcher::onMessage,disp.get(),std::placeholders::_1,std::placeholders::_2);
    
  //   auto svr = bitrpc::ServerFactory::create(9090);

  //   svr->setMessageCallback(method);
  //   svr->start();


    std::unique_ptr<bitrpc::server::ServiceFactory> desc_factory(std::make_unique<bitrpc::server::ServiceFactory>());
    desc_factory->setMethodName("Add");
    desc_factory->setParamsDesc("num1",bitrpc::server::VType::INTEGRAL);
    desc_factory->setParamsDesc("num2",bitrpc::server::VType::INTEGRAL);
    desc_factory->returnType(bitrpc::server::VType::INTEGRAL);
    desc_factory->setCallback(Add);
    bitrpc::server::RpcServer server(bitrpc::Address("127.0.0.1",9090));
    server.registerMethod(desc_factory->build());
    server.start();
    return 0;
}